
from pymongo import MongoClient
from config.config import Config

# 全局连接对象
_client = None


def get_client():
    global _client
    if _client is None:
        _client = MongoClient(Config.MONGO_URI, maxPoolSize=100)
    return _client


def create_collection_if_not_exists(db_name, collection_name):
    db = get_db(db_name)  # 获取数据库
    if collection_name not in db.list_collection_names():
        db.create_collection(collection_name)  # 如果集合不存在则创建
    return db[collection_name]


def get_db(db_name):
    return get_client()[db_name]


def get_collection(db_name,collection_name):
    return get_db(db_name)[collection_name]


def get_arkts_collection():
    arkts_functions = get_collection(Config.DB_NAME,Config.ArkTS_COLLECTION)
    return arkts_functions


def get_npm_collection():
    npm_functions = get_collection(Config.DB_NAME, Config.NPM_COLLECTION)
    return npm_functions


def get_result_collection():
    result_collection = create_collection_if_not_exists(Config.DB_NAME, Config.RESULT_COLLECTION)
    return result_collection
def get_library_info_collection():
    result_collection = get_collection(Config.DB_NAME, Config.LIBRARY_INFO_COLLECTION)
    return result_collection
def get_lib_res_collection():
    result_collection = get_collection(Config.DB_NAME, Config.lib_res_collection)
    return result_collection
def get_version_diff_info_collection():
    result_collection = get_collection(Config.DB_NAME, Config.VERSION_DIFF_COLLECTION)
    return result_collection

